import { Module, Global } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigurationService } from './services/configuration.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from './entities/user.entity';
import { UserService } from './services/user.service';
import { StartWithConstraintSync } from './validators/user-validator';
import { UtilityService } from './services/utility.service';
import { AssetService } from './services/asset.service';
import { AssetEntity } from './entities/asset.entity';
@Global()
@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    // 配置数据库链接
    TypeOrmModule.forRootAsync({
      inject: [ConfigurationService], //注入ConfigurationService服务
      useFactory: (configurationService: ConfigurationService) => {
        return {
          type: 'mysql', //链接数据库的类型
          ...configurationService.mysqlConfig,
          autoLoadEntities: true, // 自动加载所有的实体
          synchronize: true, //保持代码和数据库的一致
          logging: true, //输出内部真正sql语句
        };
      },
    }),
    TypeOrmModule.forFeature([UserEntity, AssetEntity]),
  ],
  // 如果要让一个类可以被依赖注入放到providers中
  providers: [
    ConfigurationService,
    UserService,
    StartWithConstraintSync,
    UtilityService,
    AssetService,
  ],
  exports: [
    ConfigurationService,
    UserService,
    StartWithConstraintSync,
    UtilityService,
    AssetService,
  ],
})
export class SharedModule {}
